package net.starwifi.duobao.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 夺宝订单
 * Created by ruoo on 2015/11/19.
 */
@Entity
@Table(name = "duob_db_order")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "duob_db_order_sequence")
public class DBOrder extends BaseEntity {


    private static final long serialVersionUID = 3701894495415193552L;

    /** 订单编号 */
    private String sn;

    /** 订单商品 */
    private String name;

    /** 订单商品描述 */
    private String description;

    /** 夺宝商品 */
    //private DBProduct dbProduct;

    /** 夺宝期数 */
    private DBTimes dbTimes;

    /** 夺宝用户 */
    //private Member member;

    private Long productId;

    /** 商品图片*/
    private String productMedium;

    //private Long dbTimesId;

    private Long memberId;

    /** 所属夺宝期 */
    private String timesNo;

    /** 购买份数 */
    private Integer quantity;

    /** 实际分配 */
    private Integer distributionQuantity;

    /** 付款金额*/
    private BigDecimal amount;

    /** 用户手机号 */
    private String mobile;

    /** 订单IP */
    private String ip;

    /** ip对应的市/区*/
    private String fromAddress;

    /** 订单状态（0已确认，1已完成，2已取消）*/
    private int orderStatus;

    /** 支付状态（0未支付，1已支付，2已退款,3部分支付,4部分退款）*/
    private int paymentStatus;

    /** 配送状态 （0未发货，1已发货，2已退货）*/
    private int shippingStatus;

    /** 支付方式 */
    private PaymentMethod paymentMethod;

    /** 支付方式名称 */
    private String paymentMethodName;

    /** 物流方式名称 */
    private String shippingMethodName;

    /** 物流编号 */
    private String shippingSn;

    /** 确认地址时间*/
    private Date shippingDate;

    /** 发货时间 */
    private Date shippingStartDate;

    /** 用户确认收货时间*/
    private Date shippingEndDate;

    /** 收货人 */
    private String consignee;

    /** 地区名称 */
    private String areaName;

    /** 地址 */
    private String address;

    /** 邮编 */
    private String zipCode;

    /** 电话 */
    private String phone;

    /** 用户夺宝号*/
    private Set<DBCode> dbCodeList = new HashSet<DBCode>();

    /** 是否中奖*/
    private Boolean isLottery;

    /** 用户夺宝码，使用","分割*/
    private String codeInfo;

    /** 手动退款标志*/
    private Boolean isRefund;

    /** 手动退款时间 */
    private Date refundDate;

    /** 退款信息 */
    private String refundMessage;

    //private Integer flag;

    /** 是否已经晒单*/
    private Boolean isShare;

    /** 是否是私有订单*/
    private Boolean isPrivate;



    @Pattern(regexp = "^[0-9a-zA-Z_-]+$")
    @Length(max = 100)
    @Column(nullable = false, unique = true, length = 100)
    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }


   /* @ManyToOne
    @JoinColumn(name = "db_product_id",updatable = false)
    public DBProduct getDbProduct() {
        return dbProduct;
    }

    public void setDbProduct(DBProduct dbProduct) {
        this.dbProduct = dbProduct;
    }



    @ManyToOne
    @JoinColumn(name = "member_id")
    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }*/

    @ManyToOne
    @JoinColumn(name = "db_times_id",updatable = false)
    public DBTimes getDbTimes() {
        return dbTimes;
    }

    public void setDbTimes(DBTimes dbTimes) {
        this.dbTimes = dbTimes;
    }


    @Column(name = "db_product_id")
    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

 /*   @Column
    public Long getDbTimesId() {
        return dbTimesId;
    }

    public void setDbTimesId(Long dbTimesId) {
        this.dbTimesId = dbTimesId;
    }*/

    @Column
    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    @Column
    public String getShippingMethodName() {
        return shippingMethodName;
    }


    public void setShippingMethodName(String shippingMethodName) {
        this.shippingMethodName = shippingMethodName;
    }

    @Column
    public String getShippingSn() {
        return shippingSn;
    }

    public void setShippingSn(String shippingSn) {
        this.shippingSn = shippingSn;
    }

//    @Column
//    public Integer getFlag() {
//        return flag;
//    }
//
//    public void setFlag(Integer flag) {
//        this.flag = flag;
//    }

    @Lob
    @JsonProperty
    public String getCodeInfo() {
        return codeInfo;
    }

    public void setCodeInfo(String codeInfo) {
        this.codeInfo = codeInfo;
    }


    @OneToMany(mappedBy = "dbOrder", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    public Set<DBCode> getDbCodeList() {
        return dbCodeList;
    }

    public void setDbCodeList(Set<DBCode> dbCodeList) {
        this.dbCodeList = dbCodeList;
    }


    @Min(0)
    @Column
    @JsonProperty
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }


    @Min(0)
    @Column
    public int getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus) {
        this.orderStatus = orderStatus;
    }

    @Min(0)
    @Column
    public int getPaymentStatus() {
        return paymentStatus;
    }

    public void setPaymentStatus(int paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    @Min(0)
    @Column
    public int getShippingStatus() {
        return shippingStatus;
    }

    public void setShippingStatus(int shippingStatus) {
        this.shippingStatus = shippingStatus;
    }

    @NotNull
    @ManyToOne
    public PaymentMethod getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    @Column
    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    @Column
    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    @Column
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column
    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Column
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }


    @Column
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }


    @Column
    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    @Column
    public String getPaymentMethodName() {
        return paymentMethodName;
    }

    public void setPaymentMethodName(String paymentMethodName) {
        this.paymentMethodName = paymentMethodName;
    }

    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(precision = 21, scale = 6)
    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column
    public Boolean getIsLottery() {
        return isLottery;
    }

    public void setIsLottery(Boolean isLottery) {
        this.isLottery = isLottery;
    }

    @Column
    @NotNull
    public String getTimesNo() {
        return timesNo;
    }

    public void setTimesNo(String timesNo) {
        this.timesNo = timesNo;
    }


    @Column
    public Boolean getIsRefund() {
        return isRefund;
    }

    public void setIsRefund(Boolean isRefund) {
        this.isRefund = isRefund;
    }

    @Column
    public Date getRefundDate() {
        return refundDate;
    }

    public void setRefundDate(Date refundDate) {
        this.refundDate = refundDate;
    }

    @Column
    public String getRefundMessage() {
        return refundMessage;
    }

    public void setRefundMessage(String refundMessage) {
        this.refundMessage = refundMessage;
    }

    @Column(name = "shipping_date")
    public Date getShippingDate() {
        return shippingDate;
    }

    public void setShippingDate(Date shippingDate) {
        this.shippingDate = shippingDate;
    }

    @Column(name = "shipping_start_date")
    public Date getShippingStartDate() {
        return shippingStartDate;
    }

    public void setShippingStartDate(Date shippingStartDate) {
        this.shippingStartDate = shippingStartDate;
    }

    @Column(name = "shipping_end_date")
    public Date getShippingEndDate() {
        return shippingEndDate;
    }

    public void setShippingEndDate(Date shippingEndDate) {
        this.shippingEndDate = shippingEndDate;
    }

    @Column
    public Boolean getIsShare() {
        return isShare;
    }

    public void setIsShare(Boolean isShare) {
        this.isShare = isShare;
    }

    @JsonProperty
    @Transient
    public String getIpInfo(){
        return "("+getFromAddress()+" IP:"+getIp()+")";
    }

    @JsonProperty
    @Transient
    public String getDateInfo(){
         SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm");
         return sdf.format(getCreateDate());
    }

    @Column
    public Integer getDistributionQuantity() {
        return distributionQuantity;
    }

    public void setDistributionQuantity(Integer distributionQuantity) {
        this.distributionQuantity = distributionQuantity;
    }

    @Column
    public Boolean getIsPrivate() {
        return isPrivate;
    }

    public void setIsPrivate(Boolean isPrivate) {
        this.isPrivate = isPrivate;
    }

    @Column
    public String getProductMedium() {
        return productMedium;
    }

    public void setProductMedium(String productMedium) {
        this.productMedium = productMedium;
    }

    @Transient
    public String getHtmlCodeInfo(){
        if(getCodeInfo()!=null&&getCodeInfo().contains(",")){
            String[] codeInfoList=getCodeInfo().split(",");
            StringBuffer s=new StringBuffer();
            s.append("<ul class='db-code-list'>");
            for(String code:codeInfoList){
                s.append("<li>"+code+"</li>");
            }
            return s.toString();
        }else{
            return "<ul class='db-code-list'></ul>";
        }
    }



    @JsonProperty
    @Transient
    public String getMobileInfo(){
        try {
           return getMobile().substring(0, 3) + "****" + getMobile().substring(7, getMobile().length());
        }catch (Exception e){
            return "无法获取";
        }
    }

}
